package com.lbs.jiepiaisi.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.base.BaseActivity;
import com.lbs.jiepiaisi.bean.AccountProblemEntity;
import com.lbs.jiepiaisi.biz.AccountProblemBiz;
import com.lbs.jiepiaisi.manager.SharePreferenceManager;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.ValidUtil;
import com.lbs.jiepiaisi.view.WaitingDialogControll;

/**
 *
 * 时间：2017/10/23 19:34
 * 作用：意见反馈
 */

public class AccountProblemActivity extends BaseActivity{
    private Button btn_ok;
    private AccountProblemReceiver  accountProblemReceiver ;

    public SharePreferenceManager mSharePreference=null;

    private EditText mInfoEt;
    private EditText mContractEt;
    private EditText mPhoneEt;
    private EditText mWeChatEt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isTemplate=true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_problem);
        titleView.setText("意见反馈");
        accountProblemReceiver = new AccountProblemReceiver();
        getApplicationContext().registerReceiver(accountProblemReceiver, new IntentFilter(Constant.ACCOUNT_PROBELM));
        mSharePreference=SharePreferenceManager.getInstance(this);
        findView();
        setListener();
    }


    private void findView() {
        btn_ok= (Button) findViewById(R.id.btn_ok);

        mInfoEt = (EditText)findViewById(R.id.et_info);
        mContractEt = (EditText)findViewById(R.id.et_contract);
        mPhoneEt = (EditText)findViewById(R.id.et_phone);
        mWeChatEt = (EditText)findViewById(R.id.et_wechat);
    }

    private void setListener() {

        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WaitingDialogControll.showWaitingDialog(AccountProblemActivity.this);
                final int userId = mSharePreference.getInt(Constant.ACCOUNT_USERID,-1000);
                final String info = mInfoEt.getText().toString();
                final String contract = mContractEt.getText().toString();
                final String phone = mPhoneEt.getText().toString();
                final String app  = mWeChatEt.getText().toString();
                if(userId == -1000){
                    Toast.makeText(mContext, "请重新登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                if("".equals(info)){
                    Toast.makeText(mContext, "请填写反馈信息", Toast.LENGTH_SHORT).show();
                    return;
                }
                if("".equals(contract)){
                    Toast.makeText(mContext, "请填写联系人", Toast.LENGTH_SHORT).show();
                    return;
                }
                if("".equals(phone)){
                    Toast.makeText(mContext, "请填写联系方式", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!ValidUtil.validPhone(phone)){
                    Toast.makeText(mContext, "请填写正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if("".equals(app)){
                    Toast.makeText(mContext, "请填写微信或QQ", Toast.LENGTH_SHORT).show();
                    return;
                }
                AccountProblemBiz accountProblemBiz = new AccountProblemBiz();
                accountProblemBiz.accountProblem(userId + "",info,contract,phone,app);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getApplicationContext().unregisterReceiver(accountProblemReceiver);

    }

    class AccountProblemReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            WaitingDialogControll.dismissWaitingDialog();
            AccountProblemEntity accountProblemEntity=(AccountProblemEntity) intent.
                    getSerializableExtra(Constant.KEY_DATA);
            if(accountProblemEntity.getCode()==Constant.STATUS_OK){
                if(accountProblemEntity.getStatus() == 0){
                    Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(mContext, "提交失败,请重新提交", Toast.LENGTH_SHORT).show();
                }
            }
            else if(accountProblemEntity.getCode()==Constant.STATUS_NETWORK_ERROR){
                Toast.makeText(mContext, "提交失败,请重新提交", Toast.LENGTH_SHORT).show();
            }else if(accountProblemEntity.getCode()==Constant.STATUS_NO_NET){
                Toast.makeText(mContext, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }
    }

}
